package io_nio;

import java.io.*;

/**
 * 序列化与单例
 * Created by butterfly on 2018/6/28.
 */
public class ReadSolveDemo {
    public static void main(String[] args) {
        ObjectOutputStream oos ;
        try {
            oos = new ObjectOutputStream(new FileOutputStream("E:/PP3/copyDemo.txt"));
            SingletonPerson person = SingletonPerson.getInstance();
            oos.writeObject(person);
            //反序列化
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream("E:/PP3/copyDemo.txt"));
            SingletonPerson singletonPerson = (SingletonPerson) ois.readObject();
            System.out.println(singletonPerson == person);//如果不重写readResolve()，结果是false。因为反序列化无需调用构造器也能生成对象，所以即使是单例，也是不同对象
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

}
